package cash.cmp.personal_financial_system.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
import com.alipay.service.schema.util.StringUtil;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;

public class ImageUtil {

    static Encoder encoder = Base64.getEncoder();
    static Decoder decoder = Base64.getDecoder();

    //从文件路径中获取图片转为二进制
    public static String getImageBinary(String filePath) {
        if (StringUtil.isEmpty(filePath)) {
            return null;
        }
        File f = new File(filePath);    //这里gif动态图不可以，虽然在后面也能输出gif格式，但是却不是动图
        BufferedImage bi;
        try {
            bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", baos);
            byte[] bytes = baos.toByteArray();

            return encoder.encodeToString(bytes).trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //从URl中获取图片转为二进制
    public static String getImageBinaryFromUrl(String urlPath) throws Exception {
        if (StringUtil.isEmpty(urlPath)) {
            return "";
        }
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(3000);//超时提示1秒=1000毫秒
        InputStream inStream = conn.getInputStream();//获取输出流
        byte[] data = readInputStream(inStream);
        return encoder.encodeToString(data).trim();

    }

    //readInputStream方法
    private static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];//转换为二进制
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        return outStream.toByteArray();
    }

    //将二进制转为图片
    public static void base64StringToImage(String base64String, String outFilePath) {

        if (!StringUtil.isEmpty(base64String) && !StringUtil.isEmpty(outFilePath)) {
            try {
                byte[] bytes1 = decoder.decode(base64String);
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
                BufferedImage bi1 = ImageIO.read(bais);
                File w2 = new File(outFilePath);//可以是jpg,png格式
                if (!w2.exists()) {   //文件不存在则创建文件，先创建目录
                    File dir = new File(w2.getParent());
                    dir.mkdirs();
                    w2.createNewFile(); // 创建新文件
                }
                ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片，此处不需改动
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

